package com.ironman.compute;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.common.entity.YawData;
import com.ironman.common.OriginalSimpleData;
import com.ironman.event.topic.dami.OriginalTopic;
import com.ironman.common.util.AccuracyUtil;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * 船首向计算
 */
@Slf4j
public class YawVectorComputer extends BaseVectorComputer<YawData> {

    public YawVectorComputer(Integer windowSize) {
        super(windowSize);
    }

    public void init(BiConsumer<Date, Double> yawConsumer) {
        Dami.<OriginalSimpleData, Void>bus().listen(OriginalTopic.ORIGINAL_TOPIC + POSTURE, payload -> {
            OriginalSimpleData originalSimpleData = payload.getContent();
            Date ts = originalSimpleData.getTs();
            String data = originalSimpleData.getData();
            String yaws = data.split("\\|")[1];
            List<YawData> yawDataList = Arrays.stream(yaws.split(",")).map(n -> new YawData(ts, Double.parseDouble(n))).collect(Collectors.toList());
            computeWindowData(yawDataList, yawConsumer);
        });
    }

    public void computeWindowData(List<YawData> yawDataList, BiConsumer<Date, Double> yawConsumer) {
        for (YawData yawData : yawDataList) {
            windowData(yawData, yawConsumer);
        }
    }

    public static void main(String[] args) {
        YawVectorComputer yawComputer = new YawVectorComputer(2);

        List<List<YawData>> yawDataLists = ListUtil.of(
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212),
                        new YawData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 1.212)
                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 1.111)

                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 3.111)

                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 5.111)
                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 1.111)

                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 1.111)

                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 1.111)

                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 1.111)

                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 1.111)

                ),
                ListUtil.of(
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111),
                        new YawData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 1.111)
                )
        );
        for (List<YawData> yawDataList : yawDataLists) {
            yawComputer.computeWindowData(yawDataList, new BiConsumer<Date, Double>() {
                @Override
                public void accept(Date ts, Double average) {
                    log.info("{}=>当前平均航首向{}°", ts, AccuracyUtil.downDirectionInteger(average));
                }
            });
        }
    }

}
